package com.example.demo.controller;

import com.example.demo.pojo.Order;
import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //订单详情
    @GetMapping("/findById")
    public Object findById(@RequestParam("id") Integer id) {
        HashMap<String, Object> map = new HashMap();
        Order order = orderService.findById(id);
        if (order != null) {
            map.put("code", 200);
            map.put("msg", "订单查询成功");
            map.put("data", order);
        } else {
            map.put("code", 300);
            map.put("msg", "订单查询失败");
        }
        return map;
    }

    //查询列表
    @PostMapping("/findByUserid")
    public Object findByUserid(@RequestBody Order order) {
        HashMap<String, Object> map = new HashMap();
        List<Order> list = orderService.findByUserid(order);
        if (list.size() > 0) {
            map.put("code", 200);
            map.put("msg", "订单列表查询成功");
            map.put("data", list);
        } else {
            map.put("code", 300);
            map.put("msg", "订单列表查询失败");
        }
        return map;
    }

    //修改订单
    @PostMapping("/updateOrder")
    public Object updateOrder(@RequestBody Order order) {
        HashMap<String, Object> map = new HashMap();
        int i = orderService.updateOrder(order);
        if (i > 0) {
            map.put("code", 200);
            map.put("msg", "修改订单成功");
        } else {
            map.put("code", 300);
            map.put("msg", "修改订单失败");
        }
        return map;
    }

    //添加订单
    @PostMapping("/addOrder")
    public Object addOrder(@RequestBody Order order) {
        HashMap<String, Object> map = new HashMap();
        int i = orderService.addOrder(order);
        if (i > 0) {
            map.put("code", 200);
            map.put("msg", "添加订单成功");
        } else {
            map.put("code", 300);
            map.put("msg", "添加订单失败");
        }
        return map;
    }
}
